package com.msmall.community.roster.action.html;

import java.util.List;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantAjax;
import com.msmall.community.roster.model.RosterGroup;
import com.msmall.community.roster.service.RosterManager;

public class RosterAction extends HTMLBaseAction{

	private static final long serialVersionUID = -7538438363829295116L;

	private Long messageid;//接受/拒绝好友请求时 的消息ID
	
	private Long rgid; //移动好友组时的目标组ID
	
	private Long groupid;
	
	private String name;//创建组，组名
	
	private RosterGroup rosterGroup;
	
	private String content;//请求好友时，填写的内容
	
	private List<RosterGroup> rosterGroups;
	
	private RosterManager rosterManager;
	
	/**
	 *  进入填写好友请求信息页面
	 */
	public String request(){
		if (!getIsLogin()) {
			return "unlogin";
		}
		return SUCCESS;
	}
	/**
	 * 发送好友请求
	 */
	public String ask(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			rosterManager.ask(getUserid(), getUsr(), content);
			buffer.append(createResult("true"));
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	/**
	 * 接受请求
	 */
	public String accept(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			rosterManager.accept(getUserid(), getUsr(), messageid);
			buffer.append(createResult("true"));
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	/**
	 * 拒绝请求
	 */
	public String reject(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			rosterManager.reject(getUserid(), getUsr(), messageid);
			buffer.append(createResult("true"));
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	/**
	 * 删除好友
	 */
	public String remove(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			rosterManager.removeRoster(getUserid(), getUsr());
			buffer.append(createResult("true"));
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	/**
	 * 进入移动好友组的页面，用户在此选择target 组
	 */
	public String goMoveGroup(){
		rosterGroups = rosterManager.getRosterGroupSelf(getUserid());
		return SUCCESS;
	}
	/**
	 * 移动好友的组
	 */
	public String moveGroup(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			rosterManager.moveGroup(getUserid(), getUsr(), rgid);
			buffer.append(createResult("true"));
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	/**
	 * 用户进入创建/更新好友组页面
	 */
	public String goGroup(){
		if(groupid != null && groupid.longValue() != 0l){
			rosterGroup = (RosterGroup) rosterManager.get(RosterGroup.class, groupid);
		}
		return SUCCESS;
	}
	/**
	 * 用户保存创建的/更新的好友组
	 */
	public String saveGroup(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			rosterGroup = rosterManager.saveGroup(getUserid(), rosterGroup);
			buffer.append(createResult("true"));
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Long getRgid() {
		return rgid;
	}
	public void setRgid(Long rgid) {
		this.rgid = rgid;
	}
	public Long getMessageid() {
		return messageid;
	}
	public void setMessageid(Long messageid) {
		this.messageid = messageid;
	}
	public Long getGroupid() {
		return groupid;
	}
	public void setGroupid(Long groupid) {
		this.groupid = groupid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public RosterGroup getRosterGroup() {
		return rosterGroup;
	}
	public void setRosterGroup(RosterGroup rosterGroup) {
		this.rosterGroup = rosterGroup;
	}
	public List<RosterGroup> getRosterGroups() {
		return rosterGroups;
	}
	public void setRosterGroups(List<RosterGroup> rosterGroups) {
		this.rosterGroups = rosterGroups;
	}
	public void setRosterManager(RosterManager rosterManager) {
		this.rosterManager = rosterManager;
	}
}
